package v1

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"net/http"
	"server/internal/model"
	"server/pkg/app"
)

type PNode struct {
	ID    int               `json:"id"`
	Param map[string]string `json:"param"`
}

type ReqEnv struct {
	ID      int               `json:"id"`
	Project int               `json:"project"`
	Name    string            `json:"name"`
	Nodes   []PNode           `json:"nodes"`
	Params  map[string]string `json:"params"`
}

func CreateEnv(c *gin.Context) {
	userinfo := GetUser(c)
	renv := &ReqEnv{}
	nodes := []model.MinNode{}

	_ = c.ShouldBindJSON(renv)
	if len(renv.Nodes) == 0 {
		c.String(http.StatusOK, "节点不能为空")
		return
	}
	if len(renv.Name) == 0 {
		c.String(http.StatusOK, "环境名不能为空")
		return
	}

	env := &model.Env{}
	env.Name = renv.Name
	env.ProjectID = renv.Project
	node_param := make(map[int]interface{})
	for _, item := range renv.Nodes {
		node_param[item.ID] = item.Param
		nodes = append(nodes, model.MinNode{Model: model.Model{ID: item.ID}})
	}
	node_param_str, err := json.Marshal(node_param)
	env.NodeParam = string(node_param_str)
	env.OwnerID = userinfo.ID
	env.Nodes = nodes
	param, err := json.Marshal(renv.Params)
	if err != nil {
		c.String(http.StatusOK, err.Error())
		return
	}

	env.Param = string(param)
	if err = env.Create(); err != nil {
		c.String(http.StatusOK, err.Error())
		return
	}
	c.String(http.StatusOK, "环境创建成功")
}

func GetAllEnv(c *gin.Context) {
	pages := &app.Page{}
	pages.Info(c)
	env := &model.BaseEnv{}
	env_list, total := env.List(pages)
	c.JSON(http.StatusOK, gin.H{
		"total": total,
		"data":  env_list,
	})
}

func QueryEnvInfo(c *gin.Context) {
	env := &model.Env{}
	env.Name = c.Param("name")
	err := env.Info()
	var jsonData map[string]interface{}
	err = json.Unmarshal([]byte(env.NodeParam), &jsonData)
	env.NodeParams = jsonData

	var jsonData1 map[string]interface{}
	err = json.Unmarshal([]byte(env.Param), &jsonData1)
	env.Params = jsonData1
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg": err,
		})
		return
	}
	c.JSON(http.StatusOK, env)
}

func UpdateEnv(c *gin.Context) {
	renv := &ReqEnv{}
	nodes := []model.MinNode{}

	_ = c.ShouldBindJSON(renv)
	if len(renv.Nodes) == 0 {
		c.String(http.StatusOK, "节点不能为空")
		return
	}
	if len(renv.Name) == 0 {
		c.String(http.StatusOK, "环境名不能为空")
		return
	}

	env := &model.Env{}
	env.Name = renv.Name
	env.ID = renv.ID
	env.ProjectID = renv.Project
	node_param := make(map[int]interface{})
	for _, item := range renv.Nodes {
		node_param[item.ID] = item.Param
		nodes = append(nodes, model.MinNode{Model: model.Model{ID: item.ID}})
	}
	node_param_str, err := json.Marshal(node_param)
	env.NodeParam = string(node_param_str)
	env.Nodes = nodes
	param, err := json.Marshal(renv.Params)
	if err != nil {
		c.String(http.StatusOK, err.Error())
		return
	}

	env.Param = string(param)
	if err = env.Update(); err != nil {
		c.String(http.StatusOK, err.Error())
		return
	}
	c.String(http.StatusOK, "环境修改成功")
}

func DeleteEnv(c *gin.Context) {}

func EnvTestBed(c *gin.Context) {
	env := &model.Env{}
	env.Name = c.Param("name")
	err := env.Info()
	var jsonData map[string]interface{}
	err = json.Unmarshal([]byte(env.NodeParam), &jsonData)
	env.NodeParams = jsonData

	var jsonData1 map[string]interface{}
	err = json.Unmarshal([]byte(env.Param), &jsonData1)
	env.Params = jsonData1
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg": err,
		})
		return
	}
	c.JSON(http.StatusOK, env)
}
